package com.bmf.module.rbac.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.constant.AdminResultEnum;
import com.bmf.module.rbac.entity.RbacMenuRole;
import com.bmf.module.rbac.entity.RbacRolePerm;
import com.bmf.module.rbac.mapper.RbacRolePermMapper;
import com.bmf.module.rbac.service.RbacRolePermService;
import com.bmf.module.rbac.vo.RbacMenuRoleCreateVo;
import com.bmf.module.rbac.vo.RbacRolePermCreateVo;
import com.bmf.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (RbacRolePerm)表服务实现类
 *
 * @author makejava
 * @since 2022-12-27 15:33:18
 */
@Service("rbacRolePermService")
public class RbacRolePermServiceImpl extends ServiceImpl<RbacRolePermMapper, RbacRolePerm> implements RbacRolePermService {

    @Override
    public List<Integer> selectAll(Integer roleId) {
        LambdaQueryWrapper<RbacRolePerm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacRolePerm::getRoleid, roleId);
        List<RbacRolePerm> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.stream().map(RbacRolePerm::getPermid).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(RbacRolePermCreateVo rbacRolePermCreateVo) {
        List<Integer> permids = rbacRolePermCreateVo.getPermids();
        LambdaQueryWrapper<RbacRolePerm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacRolePerm::getRoleid, rbacRolePermCreateVo.getRoleid());
        if(CollUtil.isEmpty(permids)){
            remove(wrapper);
            return;
        }
        List<RbacRolePerm> list = list(wrapper);
//        if(CollUtil.isNotEmpty(list)){
//            List<Integer> menuIds2 = list.stream().map(RbacMenuRole::getMenuid).collect(Collectors.toList());
//            Collections.sort(menuIds2);
//            Collections.sort(menuIds);
//            if(!menuIds2.toString().equals(menuIds.toString())){
//                LambdaQueryWrapper<RbacMenuRole> wrapper1 = new LambdaQueryWrapper();
//                wrapper1.eq(RbacMenuRole::getRoleid, rbacMenuRoleUpdateVo.getRoleId());
//                remove(wrapper1);
//                boolean b = saveBatch(BeanCopyUtils.copyBeanList(rbacMenuRoleCreateVos, RbacMenuRole.class));
//                AdminResultEnum.CREATE_EXCEPTION.assertIsTrue(b);
//            }
//        }
    }
}
